package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.redis.cache.GuiguCache;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.*;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @Author ChenXing
 * @Description
 * @Create 2024/7/29 11:01
 */

@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    @Autowired
    private RedissonClient redissonClient;


    @Autowired
    private ICategoryBrandService categoryBrandService;


    @Operation(summary = "查询商品列表")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {

        int i = productService.insertProduct(product);
        return toAjax(i);
    }

    @Operation(summary = "获取商品详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Product product = productService.selectProductById(id);
        return success(product);
    }

    @Operation(summary = "修改商品")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {

        int i = productService.updateProduct(product);

        return toAjax(i);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        int i = productService.deleteProductByIds(ids);
        return toAjax(i);
    }


    @Operation(summary = "商品审核")
    @GetMapping("updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return success();
    }

    @Operation(summary = "更新上下架状态")
    @GetMapping("updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return success();
    }

    @InnerAuth
    @GetMapping("/getTopSale")
    public R<List<ProductSku>> getTopSale() {

        List<ProductSku> productSkuList = productService.getTopSale();
        return R.ok(productSkuList);
    }

    @InnerAuth
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(
            @Parameter(name = "pageNum", description = "当前页码", required = true)
            @PathVariable Integer pageNum,

            @Parameter(name = "pageSize", description = "每页记录数", required = true)
            @PathVariable Integer pageSize,

            @Parameter(name = "productQuery", description = "查询对象", required = false)
            @ModelAttribute SkuQuery skuQuery) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSku> list = productService.selectProductSkuList(skuQuery);
        return R.ok(getDataTable(list));
    }





    @Operation(summary = "获取商品sku信息")
    @InnerAuth
    @GetMapping("/getProductSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable("skuId") Long skuId) {

        // 概率器
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter("sku:bloom:filter");
        boolean contains = bloomFilter.contains(skuId);// 判断mysql中是否有skuId
        ProductSku productSku = null;
        if(contains){
            productSku = productService.getProductSku(skuId);
        }
        return R.ok(productSku);

    }

    @Operation(summary = "获取商品product信息")
    @InnerAuth
    @GetMapping("/getProduct/{skuId}")
    public R<Product> getProduct(@PathVariable("skuId") Long skuId) {

        Product product = productService.getProduct(skuId);

        return R.ok(product);
    }

    @Operation(summary = "获取商品SkuPrice信息")
    @InnerAuth
    @GetMapping("/getSkuPrice/{skuId}")
    public R<SkuPrice> getSkuPrice(@PathVariable("skuId") Long skuId) {

        SkuPrice skuPrice = productService.getSkuPrice(skuId);

        return R.ok(skuPrice);
    }

    @Operation(summary = "获取商品ProductDetails信息")
    @InnerAuth
    @GetMapping("/getProductDetails/{skuId}")
    public R<ProductDetails> getProductDetails(@PathVariable("skuId") Long skuId) {

        ProductDetails productDetails = productService.getProductDetails(skuId);

        return R.ok(productDetails);
    }

    @Operation(summary = "获取商品SkuStockVo信息")
    @InnerAuth
    @GetMapping("/getSkuStockVo/{skuId}")
    public R<SkuStockVo> getSkuStockVo(@PathVariable("skuId") Long skuId) {

        SkuStockVo skuStockVo = productService.getSkuStockVo(skuId);

        return R.ok(skuStockVo);
    }

    @Operation(summary = "获取商品ProductSkuList信息")
    @InnerAuth
    @GetMapping("/getProductSkuList/{id}")
    public R<List<ProductSku>> ProductSkuList(@PathVariable("id") Long id) {

        List<ProductSku> productSkuList = productService.ProductSkuList(id);

        return R.ok(productSkuList);
    }

    @Operation(summary = "获取商品SkuPriceList信息")
    @InnerAuth
    @PostMapping("/getSkuPriceList")
    public R<List<SkuPrice>> getSkuPriceList(@RequestBody List<Long> skuIds){

        List<SkuPrice> skuPriceList = productService.getSkuPriceList(skuIds);

        return R.ok(skuPriceList);
    }


    @Operation(summary = "检查与锁定库存")
    @InnerAuth
    @PostMapping("/checkAndLock/{tradeNo}")
    public R<String> checkAndLock(@PathVariable String tradeNo, @RequestBody List<SkuLockVo> skuLockVoList){

     String errResult =   productService.checkAndLock(tradeNo, skuLockVoList);
     return R.ok(errResult);

    }
}
